package com.apotik.legenda.dao;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.hibernate.Criteria;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.apotik.legenda.model.Customer;
import com.apotik.legenda.util.HibernateUtil;

public class CustomerDao {

	public boolean addNewOrUpdate(Customer obj){
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.saveOrUpdate(obj);
			session.getTransaction().commit();
			session.close();
			return true;
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
			return false;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Customer> listByActive(boolean isActive){
		List<Customer> list = new ArrayList<>();
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			Criteria cr = session.createCriteria(Customer.class);
			cr.add(Restrictions.eq("isActive", isActive));
			list = cr.list();
			
			session.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Customer> listByActiveLimit(boolean isActive, int first, int max){
		List<Customer> list = new ArrayList<>();
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			Criteria cr = session.createCriteria(Customer.class);
			cr.add(Restrictions.eq("isActive", isActive));
			cr.setFirstResult(first);
			cr.setMaxResults(max);
			cr.addOrder(Order.asc("customerId"));
			list = cr.list();
			
			session.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return list;
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Customer> search(boolean isActive, String param){
		List<Customer> list = new ArrayList<>();
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria cr = session.createCriteria(Customer.class);
			
			Criterion _code = Restrictions.ilike("phoneNumber", "%"+param+"%");
			Criterion _name = Restrictions.ilike("fullName", "%"+param+"%");
			
			LogicalExpression orExp = Restrictions.or(_code, _name);
			cr.add(orExp);
			
			list = cr.list();
			
			session.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return list;
	}
	
	@SuppressWarnings({ "unchecked" })
	public Customer byNameAndHandphone(String name, String handphone){
		Customer customer = null;
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			
			Criteria cr = session.createCriteria(Customer.class);
			cr.add(Restrictions.eq("fullName", name));
			cr.add(Restrictions.eq("phoneNumber", handphone));
			
			List<Customer> list = cr.list();
			if(list!=null){
				if(list.size()>0){
					customer = list.get(0);
				}
			}
			
			
			session.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return customer;
	}
	
	@SuppressWarnings({ "rawtypes" })
	public int size(boolean activeCount, boolean activeValue){
		int result = 0;
		
		try {
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria cr = session.createCriteria(Customer.class);
			if(activeCount){
				cr.add(Restrictions.eq("isActive", activeValue));
			}
			cr.setProjection(Projections.rowCount());
			List list = cr.list();
			
			if(list!=null){
				result = (int) list.get(0);
			}
			
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
		return result;
	}
	
}
